<?php

/**
 * ECSHOP 商品类型管理程序
 * ============================================================================
 * 版权所有 2005-2010 上海商派网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.ecshop.com；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: liuhui $
 * $Id: data_package.php 17063 2010-03-25 06:35:46Z tom $
*/

define('IN_ECS', true);
define('DATA_PACKAGE_DIR','../data_package/');

require(dirname(__FILE__) . '/includes/init.php');

$exc = new exchange($ecs->table("data_package"), $db, 'package_id', 'name');

/*------------------------------------------------------ */
//-- 管理界面
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'list')
{
    assign_query_info();

    $smarty->assign('ur_here',          $_LANG['01_package']);
    $smarty->assign('full_page',        1);

    $package_list = get_packages();
 

    $smarty->assign('package_list',   $package_list['type']);
    $smarty->assign('filter',       $package_list['filter']);
    $smarty->assign('record_count', $package_list['record_count']);
    $smarty->assign('page_count',   $package_list['page_count']);


    $smarty->assign('action_link',      array('text' => $_LANG['new_package'], 'href' => 'data_package.php?act=add'));

    $smarty->display('data_package.htm');
}

/*------------------------------------------------------ */
//-- 获得列表
/*------------------------------------------------------ */

elseif ($_REQUEST['act'] == 'query')
{
    $good_type_list = get_packages();

    $smarty->assign('package_list',   $package_list['type']);
    $smarty->assign('filter',       $package_list['filter']);
    $smarty->assign('record_count', $package_list['record_count']);
    $smarty->assign('page_count',   $package_list['page_count']);

    make_json_result($smarty->fetch('data_package.htm'), '',
        array('filter' => $good_type_list['filter'], 'page_count' => $good_type_list['page_count']));
}


/*------------------------------------------------------ */
//-- 添加商品类型
/*------------------------------------------------------ */

elseif ($_REQUEST['act'] == 'add')
{

    $smarty->assign('ur_here',     $_LANG['new_package']);
    $smarty->assign('action_link', array('href'=>'data_package.php?act=list', 'text' => $_LANG['view_packagelist']));
    $smarty->assign('action',      'add');
    $smarty->assign('form_act',    'insert');


    assign_query_info();
    $smarty->display('data_package_info.htm');
}

elseif ($_REQUEST['act'] == 'insert')
{
	$found_error = false;
	$package = Array();
    $package['name']   = sub_str($_POST['package_name'], 255);
	$package['type']   = sub_str($_POST['package_type'], 50);
    $package['pai_xu'] = sub_str($_POST['pai_xu'], 255);
    $package['description'] = sub_str($_POST['package_description'], 255);
	 $package['suppliers_id'] =  $_SESSION['supply_id'];
	//upload file
	if($_FILES['package_file'] && $_FILES["package_file"]["name"])
	{
		$file_name = $_FILES["package_file"]["name"];
		
		if(empty($package['name']))
		{
			$package['name'] = $file_name;
		}
		
		$file_ext = strrchr($file_name,".");
		$file_dir_name = time().$file_ext;
		$file_path = DATA_PACKAGE_DIR.$file_dir_name;
		$package['file'] = $file_dir_name;
		if (!file_exists(DATA_PACKAGE_DIR))
		{
			make_dir(DATA_PACKAGE_DIR);
		}
		move_uploaded_file($_FILES["package_file"]["tmp_name"],$file_path);
		if(!file_exists($file_path))
		{
			$found_error = true;
			sys_msg($_LANG['add_data_package_failed'], 1);
		}
	}
	
    if (!$found_error && $db->autoExecute($ecs->table('data_package'), $package) !== false)
    {
        $links = array(array('href' => 'data_package.php?act=list', 'text' => $_LANG['back_list']));
        sys_msg($_LANG['add_data_package_success'], 0, $links);
    }
    else
    {
        sys_msg($_LANG['add_data_package_failed'], 1);
    }
}

/*------------------------------------------------------ */
//-- 编辑商品类型
/*------------------------------------------------------ */

elseif ($_REQUEST['act'] == 'edit')
{
    $package = get_data_package_info(intval($_GET['package_id']));

    if (empty($package))
    {
        sys_msg($_LANG['cannot_found_data_package'], 1);
    }

   

    $smarty->assign('ur_here',     $_LANG['eidt_package']);
    $smarty->assign('action_link', array('href'=>'data_package.php?act=list', 'text' => $_LANG['view_packagelist']));
    $smarty->assign('action',      'add');
    $smarty->assign('form_act',    'update');
    $smarty->assign('package',  $package);

    assign_query_info();
    $smarty->display('data_package_info.htm');
}

elseif ($_REQUEST['act'] == 'update')
{

$found_error = false;
	$package = Array();
    $package['name']   = sub_str($_POST['package_name'], 255);
	$package['type']   = sub_str($_POST['package_type'], 50);
    $package['pai_xu'] = sub_str($_POST['pai_xu'], 255);
    $package['description'] = sub_str($_POST['package_description'], 255);
	//upload file
	if($_FILES['package_file'] && $_FILES["package_file"]["name"])
	{
		$file_name = $_FILES["package_file"]["name"];
		
		if(empty($package['name']))
		{
			$package['name'] = $file_name;
		}
		
		$file_ext = strrchr($file_name,".");
		$file_dir_name = time().$file_ext;
		$file_path = DATA_PACKAGE_DIR.$file_dir_name;
		$package['file'] = $file_dir_name;
		if (!file_exists(DATA_PACKAGE_DIR))
		{
			make_dir(DATA_PACKAGE_DIR);
		}
		move_uploaded_file($_FILES["package_file"]["tmp_name"],$file_path);
		if(!file_exists($file_path))
		{
			$found_error = true;
			sys_msg($_LANG['edit_data_package_failed'], 1);
		}
	}
	$package_id                   = intval($_POST['package_id']);
	
	//remove file
	$package = get_data_package_info($package_id);
	if(empty($package))
	{
		exit("Bad access");
	}
    if (!$found_error && $db->autoExecute($ecs->table('data_package'), $package, 'UPDATE', "package_id='$package_id'") !== false)
    {
        $links = array(array('href' => 'data_package.php?act=list', 'text' => $_LANG['back_list']));
        sys_msg($_LANG['edit_data_package_success'], 0, $links);
    }
    else
    {
        sys_msg($_LANG['edit_data_package_failed'], 1);
    }
	
}

/*------------------------------------------------------ */
//-- 删除商品类型
/*------------------------------------------------------ */

elseif ($_REQUEST['act'] == 'remove')
{
   // check_authz_json('goods_type');
     $id = intval($_GET['id']);
	//remove file
	$package = get_data_package_info($id);
	if(empty($package))
	{
		make_json_error($_LANG['remove_failed']);
		exit();
	}
	$file_path = DATA_PACKAGE_DIR.$package['file'];
  	@unlink($file_path);
    
    $name = $exc->get_name($id);

    if ($exc->drop($id))
    {

        $url = 'data_package.php?act=query&' . str_replace('act=remove', '', $_SERVER['QUERY_STRING']);

        ecs_header("Location: $url\n");
    }
    else
    {
       make_json_error($_LANG['remove_failed']);
	   exit();
    }
}


/**
 * 获得所有数据包
 *
 * @access  public
 * @return  array
 */
function get_packages()
{
    $result = get_filter();

    if ($result === false)
    {
        /* 分页大小 */
        $filter = array();

        /* 记录总数以及页数 */
        $sql = "SELECT COUNT(*) FROM ".$GLOBALS['ecs']->table('data_package').
        		" WHERE suppliers_id=".$_SESSION['supply_id'];
        $filter['record_count'] = $GLOBALS['db']->getOne($sql);

        $filter = page_and_size($filter);

        /* 查询记录 */
        $sql = "SELECT *".
               "FROM ". $GLOBALS['ecs']->table('data_package').
               " WHERE suppliers_id=".$_SESSION['supply_id'].
               " ORDER BY `pai_xu` ".
               'LIMIT ' . $filter['start'] . ',' . $filter['page_size'];
        set_filter($filter, $sql);
    }
    else
    {
        $sql    = $result['sql'];
        $filter = $result['filter'];
    }

    $all = $GLOBALS['db']->getAll($sql);

    foreach ($all AS $key=>$val)
    {
        $all[$key]['attr_group'] = strtr($val['attr_group'], array("\r" => '', "\n" => ", "));
    }

    return array('type' => $all, 'filter' => $filter, 'page_count' => $filter['page_count'], 'record_count' => $filter['record_count']);
}

/**
 * 获得指定的商品类型的详情
 *
 * @param   integer     $cat_id 分类ID
 *
 * @return  array
 */
function get_data_package_info($package_id)
{
    $sql = "SELECT * FROM " .$GLOBALS['ecs']->table('data_package'). " WHERE package_id='$package_id' AND `suppliers_id`=".$_SESSION['supply_id'];
	

    return $GLOBALS['db']->getRow($sql);
}

/**
 * 更新属性的分组
 *
 * @param   integer     $cat_id     商品类型ID
 * @param   integer     $old_group
 * @param   integer     $new_group
 *
 * @return  void
 */
function update_attribute_group($cat_id, $old_group, $new_group)
{
    $sql = "UPDATE " . $GLOBALS['ecs']->table('attribute') .
            " SET attr_group='$new_group' WHERE cat_id='$cat_id' AND attr_group='$old_group'";
    $GLOBALS['db']->query($sql);
}

?>
